package util.converter;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import util.enumeration.IsHalal;
import util.enumeration.Language;
import util.enumhelper.SupportedLocale;
import util.localization.WebLocalization;

@FacesConverter(value = "halalTypeConverter", forClass = util.enumeration.IsHalal.class)
public class HalalTypeConverter implements Converter {
    private WebLocalization localization;
    private String preferredLocale;
    
    public HalalTypeConverter() {
        /* LOCALIZATION */
        localization = new WebLocalization();
        /* END LOCALIZATION */
    }
    
    //getAsObject method converts a string to a an object
    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {    

        preferredLocale = localization.getPreferredLocaleFromSession();
                
        if (value == null || value.length() == 0) {
            return null;
        }
    
        // en_US ENGLISH, UNITED STATES
        if (preferredLocale.equals(SupportedLocale.getSupportedLocaleStringByLanguage(Language.ENGLISH_US))) {
            return getHalalTypeAsObject_EN_US(value);
        }
        // en_SG ENGLISH, SINGAPORE
        else
        if (preferredLocale.equals(SupportedLocale.getSupportedLocaleStringByLanguage(Language.ENGLISH_SINGAPORE))) {
            return getHalalTypeAsObject_EN_SG(value);
        }
        // ms_MY MALAY, MALAYSIA
        else
        if (preferredLocale.equals(SupportedLocale.getSupportedLocaleStringByLanguage(Language.MALAY))) {
            return getHalalTypeAsObject_MS_MY(value);
        }

        // zh_CN CHINESE, CHINA
        else
        if (preferredLocale.equals(SupportedLocale.getSupportedLocaleStringByLanguage(Language.CHINESE_CHINA))) {
            return getHalalTypeAsObject_ZH_CN(value);
        }
        // other language...
        else {
            return getHalalTypeAsObject_EN_US(value);
        }
    }
    
    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        
        preferredLocale = localization.getPreferredLocaleFromSession();
        
        if (value == null) {
            return null;
        }
  
        if (value instanceof IsHalal) {
            IsHalal halalType = (IsHalal) value;
            
            String localizedHalalType = "";
            
            // en_US ENGLISH, UNITED STATES
            if (preferredLocale.equals(SupportedLocale.getSupportedLocaleStringByLanguage(Language.ENGLISH_US))) {
                localizedHalalType = getHalalTypeAsString_EN_US(halalType);
            }
            // en_SG ENGLISH, SINGAPORE
            else
            if (preferredLocale.equals(SupportedLocale.getSupportedLocaleStringByLanguage(Language.ENGLISH_SINGAPORE))) {
                localizedHalalType = getHalalTypeAsString_EN_SG(halalType);
            }
            // ms_MY MALAY, MALAYSIA
            else
            if (preferredLocale.equals(SupportedLocale.getSupportedLocaleStringByLanguage(Language.MALAY))) {
                localizedHalalType = getHalalTypeAsString_MS_MY(halalType);
            }

            // zh_CN CHINESE, CHINA
            else
            if (preferredLocale.equals(SupportedLocale.getSupportedLocaleStringByLanguage(Language.CHINESE_CHINA))) {
                localizedHalalType = getHalalTypeAsString_ZH_CN(halalType);
            }
            // other language...
            else {
                localizedHalalType = getHalalTypeAsString_EN_US(halalType);
            }

            return localizedHalalType;
        }
        else {
            throw new IllegalArgumentException("object " + value + " is of type " + value.getClass().getName() + "; expected type: util.enumeration.CuisineType");
        }
    }
    
    // simplified chinese (zh_CN)
    private static Object getHalalTypeAsObject_ZH_CN(String value) {
        if(value.equals("是")) return IsHalal.YES;
        else return IsHalal.NO;
    }
    
    // default (en_US)
    private static Object getHalalTypeAsObject_EN_US(String value) {
        if(value.equals("Yes")) return IsHalal.YES;
        else return IsHalal.NO;

    }
    
    // singapore english (en_SG)
    private static Object getHalalTypeAsObject_EN_SG(String value) {
        if(value.equals("Yes")) return IsHalal.YES;
        else return IsHalal.NO;
    }
    
    // bahasa malaysia (ms_MY)
    private static Object getHalalTypeAsObject_MS_MY(String value) {
       if(value.equals("Ya")) return IsHalal.YES;
       else return IsHalal.NO;
    }
    
    // simplified chinese (zh_CN)
    private static String getHalalTypeAsString_ZH_CN(IsHalal halalType) {
        String localizedHalalType = "";
        
        switch (halalType) {
            case YES: localizedHalalType = "是" ; break;
            case NO: localizedHalalType = "不是"; break;
        }
        return localizedHalalType;
    }
    
    // default (en_US)
    private static String getHalalTypeAsString_EN_US(IsHalal halalType) {
        String localizedHalalType = "";
        
        switch (halalType) {
            case YES: localizedHalalType = "Yes" ; break;
            case NO: localizedHalalType = "No"; break;
        }
        return localizedHalalType;
    }
    
    // singapore english (en_SG)
    private static String getHalalTypeAsString_EN_SG(IsHalal halalType) {
        String localizedHalalType = "";
        
        switch (halalType) {
            case YES: localizedHalalType = "Yes" ; break;
            case NO: localizedHalalType = "No"; break;
        }
        return localizedHalalType;
    }
    
    // bahasa malaysia (ms_MY)
    private static String getHalalTypeAsString_MS_MY(IsHalal halalType) {
        String localizedHalalType = "";
        
        switch (halalType) {
            case YES: localizedHalalType = "Ya" ; break;
            case NO: localizedHalalType = "Wanita"; break;
        }
        return localizedHalalType;
    }
}